<?php

declare(strict_types=1);
/**
 * This file is part of MoChat.
 * @link     https://mo.chat
 * @document https://mochat.wiki
 * @contact  group@mo.chat
 * @license  https://github.com/mochat-cloud/mochat/blob/master/LICENSE
 */
namespace MoChat\Plugin\ContactTodo\Task;

use EasyWeChat\Kernel\Exceptions\InvalidConfigException;
use GuzzleHttp\Exception\GuzzleException;
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\Crontab\Annotation\Crontab;
use Hyperf\Di\Annotation\Inject;
use MoChat\App\Corp\Contract\CorpContract;
use MoChat\Plugin\ContactTodo\Queue\TodoRemindQueue;

/**
 * @Crontab(name="TodoRemindTask", rule="*\/1 * * * *", callback="execute", singleton=true, memo="企业微信-待办提醒")
 */
class TodoRemindTask
{
    /**
     * @Inject
     * @var TodoRemindQueue
     */
    protected $todoRemindQueue;

    /**
     * @Inject
     * @var CorpContract
     */
    protected $corpService;

    /**
     * @Inject
     * @var StdoutLoggerInterface
     */
    private $logger;

    public function execute(): void
    {
        $this->logger->info('待办提醒开始');
        $corp = $this->corpService->getCorps(['id']);
        foreach ($corp as $item) {
            // 异步处理
            try {
                $this->todoRemindQueue->handle($item['id']);
            } catch (InvalidConfigException|GuzzleException $e) {
                $this->logger->error('待办提醒异步调用失败' . $e->getMessage());
            }
        }
        $this->logger->info('待办提醒完成');
    }
}
